package com.ruoyi.web.utils;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Ccompany;
import com.ruoyi.system.domain.SysSchool;

import java.util.ArrayList;
import java.util.List;

public class PDFCVUtils {

    private static boolean hasBr(String str) {
        if (str.contains("工作经验") || str.contains("工作经历") ||
                str.contains("项目经验") || str.contains("项目经历") ||
                str.contains("教育背景") || str.contains("教育经历") ||
                str.contains("特长") || str.contains("评价") || str.contains("基本资料")) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean hasCompany(String str) {
        if (str.contains("公司") || str.contains("银行") || str.contains("单位") ||
                str.contains("研究所") || str.contains("研究院") || str.contains("大学") ||
                str.contains("学院") || str.contains("机构")) {
            return true;
        } else {
            return false;
        }
    }

    public static List<Ccompany> getCompany(String str) {
        List<Ccompany> ccompanyList = new ArrayList<>();
        String descr = "";
        String[] brArr = str.split("\r?\n");
        if (brArr.length > 0) {
            for (int i = 0; i < brArr.length; i++) {
                if (brArr[i].contains("工作经验") ||
                        brArr[i].contains("工作经历")) {
                    // i 行以下 i+1
                    for (int j = (i + 1); j < brArr.length; j++) {
                        if (StringUtils.isBlank(brArr[j])) {
                            continue;
                        }
                        String eduStr = brArr[j];
                        if (eduStr.length() > 6 || !hasBr(eduStr)) {
                            String[] eduArr = eduStr.split(" ");
                            if ((!eduStr.contains("，") &&
                                    !eduStr.contains("；") &&
                                    !eduStr.contains("。")) &&
                                    hasCompany(eduStr)) {
                                Ccompany ccompany = new Ccompany();
                                if (ccompanyList != null &&
                                        ccompanyList.size() > 0) {
                                    ccompanyList.get(ccompanyList.size() - 1).setDescription(descr);
                                }
                                descr = "";
                                if (eduArr.length >= 2) {
                                    for (int k = 0; k < eduArr.length; k++) {
                                        if (hasCompany(eduStr)) {
                                            ccompany.setCom(eduArr[k]);
                                        } else if (!eduArr[k].matches(".*\\d.*")) {
                                            ccompany.setTitle(eduArr[k]);
                                        }
                                    }
                                    ccompanyList.add(ccompany);
                                } else {
                                    if (hasCompany(eduStr)) {
                                        ccompany.setCom(eduStr);
                                    }
                                    for (int r = (j + 1); r < (j + 4); r++) {
                                        if (!brArr[r].matches(".*\\d.*")) {
                                            ccompany.setTitle(brArr[r]);
                                            break;
                                        }
                                    }
                                    ccompanyList.add(ccompany);
                                }
                            } else {
                                descr = descr + eduStr;
                            }
                        } else {
                            break;
                        }
                    }
                    break;
                } else {

                }
            }
            if (ccompanyList != null &&
                    ccompanyList.size() > 0) {
                ccompanyList.get(ccompanyList.size() - 1).setDescription(descr);
            }
            return ccompanyList;
        } else {
            return null;
        }
    }

    public static List<SysSchool> getEducation(String str) {
        List<SysSchool> sysSchoolList = new ArrayList<>();
        String descr = "";
        String[] brArr = str.split("\r?\n");
        if (brArr.length > 0) {
            for (int i = 0; i < brArr.length; i++) {
                if (brArr[i].contains("教育背景") ||
                        brArr[i].contains("教育经历")) {
                    // i 行以下 i+1
                    for (int j = (i + 1); j < brArr.length; j++) {
                        String eduStr = brArr[j];
                        if (eduStr.length() > 10) {
                            String[] eduArr = eduStr.split(" ");
                            if (eduArr.length >= 2 && (eduStr.contains("大学") ||
                                    eduStr.contains("学院") ||
                                    eduStr.contains("中学"))) {
                                SysSchool sysSchool = new SysSchool();
                                if (sysSchoolList != null && sysSchoolList.size() > 0) {
                                    sysSchoolList.get(sysSchoolList.size() - 1).setDescription(descr);
                                }
                                descr = "";
                                if (eduStr.contains("本科")) {
                                    sysSchool.setDegree("本科");
                                } else if (eduStr.contains("硕士")) {
                                    sysSchool.setDegree("硕士");
                                } else if (eduStr.contains("博士")) {
                                    sysSchool.setDegree("博士");
                                }
                                for (int k = 0; k < eduArr.length; k++) {
                                    if (eduArr[k].contains("大学") ||
                                            eduArr[k].contains("学院") ||
                                            eduArr[k].contains("中学")) {
                                        sysSchool.setName(eduArr[k]);
                                    } else if (eduArr[k].contains("专业") ||
                                            eduArr[k].contains("工程") ||
                                            eduArr[k].contains("学")) {
                                        sysSchool.setMajor(eduArr[k]);
                                    }
                                }
                                sysSchoolList.add(sysSchool);
                            } else {
                                descr = descr + eduStr;
                            }
                        } else {
                            break;
                        }
                    }
                    break;
                } else {
                }
            }
            if (sysSchoolList != null && sysSchoolList.size() > 0) {
                SysSchool sysSchool = sysSchoolList.get(sysSchoolList.size() - 1);
                sysSchool.setDescription(descr);
            }
            return sysSchoolList;
        } else {
            return null;
        }
    }

    public static String getOther(String str) {
        String descr = "";
        String[] brArr = str.split("\r?\n");
        if (brArr.length > 0) {
            for (int i = 0; i < brArr.length; i++) {
                if (brArr[i].contains("特长") ||
                        brArr[i].contains("评价")) {
                    // i 行以下 i+1
                    for (int j = (i + 1); j < brArr.length; j++) {
                        String eduStr = brArr[j];
                        if (eduStr.length() > 6) {
                            descr = descr + eduStr;
                        } else {
                            break;
                        }
                    }
                    break;
                } else {
                }
            }
            return descr;
        } else {
            return null;
        }
    }

    public static String getName(String str){
        String[] split = str.split("名：");
        if (split.length <= 1) {
            String[] lines = str.split("\r?\n"); // 根据换行符分隔成多行数组
            if (lines.length > 0) {
                for (int i = 0; i < 8; i++) {
                    String nameLine = lines[i].split(" ")[0];
                    if (StringUtils.isBlank(nameLine)) {
                        continue;
                    }
                    if (nameLine.length() > 5) {
                    } else {
                        return nameLine;
                    }
                }
                return "";
            } else {
                return "";
            }
        } else {
            String nameStr = split[1].trim().split(" ")[0];
            return nameStr;
        }
    }

    public static String getGender(String str){
        if (str.contains("男")) {
            return "M";
        } else if (str.contains("女")) {
            return "F";
        } else {
            return "U";
        }
    }

    public static String getEmail(String str){
        String[] split = str.split("件：");
        if (split.length <= 1) {
            String[] lines = str.split("箱："); // 根据换行符分隔成多行数组

            if (lines.length <= 1) {
                return "";
            } else {
                String nameStr = lines[1].trim().split(" ")[0];
                String[] brArr = nameStr.split("\r?\n");
                if (brArr.length > 0) {
                    return brArr[0];
                } else {
                    return nameStr;
                }

            }
        } else {
            String nameStr = split[1].trim().split(" ")[0];
            String[] brArr = nameStr.split("\r?\n");
            if (brArr.length > 0) {
                return brArr[0];
            } else {
                return nameStr;
            }
        }
    }

    public static String getPhone(String str){
        String[] split = str.split("话：");
        if (split.length <= 1) {
            String[] lines = str.split("机："); // 根据换行符分隔成多行数组

            if (lines.length <= 1) {
                return "";
            } else {
                String nameStr = lines[1].trim().split(" ")[0];
                String[] brArr = nameStr.split("\r?\n");
                if (brArr.length > 0) {
                    return brArr[0];
                } else {
                    return nameStr;
                }

            }
        } else {
            String nameStr = split[1].trim().split(" ")[0];
            String[] brArr = nameStr.split("\r?\n");
            if (brArr.length > 0) {
                return brArr[0];
            } else {
                return nameStr;
            }
        }
    }

}
